package ljl.codetop300;

import commons.TreeNode;

public class _572_is_subtree {

  /**
   * 这个递归的比较每个节点，比较树是另一个方法
   */
  public boolean isSubtree(TreeNode root, TreeNode subRoot) {
    if (root == null) return subRoot == null;
    return contains(root, subRoot) || isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
  }

  /**
   * 这个用来比较两棵树
   */
  boolean contains(TreeNode root, TreeNode subRoot) {
    if (root == null) return subRoot == null;
    if (subRoot == null) return false;
    return root.val == subRoot.val && contains(root.left, subRoot.left) && contains(root.right, subRoot.right);
  }

}
